package com.gemme.gemme.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gemme.gemme.setting.BIgPIctureActivity;
import com.gemme.gemme.setting.GmWebActivity;
import com.gemme.gemme.view.BigPictureDialog;
import com.gemme.gemme.view.HomeBannerBean;
import com.gemme.gemme.view.TuijianguanDialog;
import com.gemme.gemme.wode.Tuijianguan2Activity;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class Image2Adapter extends BannerAdapter<DataBean, Image2Adapter.BannerViewHolder> {

    private HomeBannerBean homeBannerBean;

    public Image2Adapter(List<DataBean> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(parent.getContext());
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerViewHolder(imageView);
    }

    @Override
    public void onBindView(BannerViewHolder holder, DataBean data, int position, int size) {
//        holder.imageView.setImageResource(data.imageRes);
        try {
            Context context = holder.itemView.getContext();
            if(!TextUtils.isEmpty(data.getImageRes())){
                Glide.with(holder.itemView.getContext()).load(data.getImageRes()).into( holder.imageView);
            }else{
                Glide.with(holder.itemView.getContext()).load(data.getNativeimage()).into( holder.imageView);
            }

            int id = data.getId();
            int ismast = getMast(id);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(ismast == 0){//显示大图
                        String bigImage = getBigImage(id);
                       /* Intent intent = new Intent(context, GmWebActivity.class);
                        intent.putExtra("data",bigImage);
                        context.startActivity(intent);*/

                      /*  TuijianguanDialog tuijianguanDialog = new TuijianguanDialog();
                        tuijianguanDialog.setUrl("http://47.114.162.17/protocol/index.html?type=protoco3");
                        tuijianguanDialog.showDialog(mContext);*/
                       /* if(TextUtils.isEmpty(bigImage))
                            return;

                        BigPictureDialog bigPictureDialog = new BigPictureDialog();
                        bigPictureDialog.setUrl(bigImage);
                        bigPictureDialog.showDialog(context);*/

                        Intent intent = new Intent(context, BIgPIctureActivity.class);
                        intent.putExtra("data",bigImage);
                        context.startActivity(intent);
                    }else if(ismast == 100000){//进入成为推荐官
                        context.startActivity(new Intent(context, Tuijianguan2Activity.class));
                    }
                }
            });




        }catch (Exception e){

        }
    }

    private String getBigImage(int id) {
        String picUrl = "";
        try {
            if(homeBannerBean ==null)
                return picUrl;

            List<HomeBannerBean.DataBean.RecordsBean> records = homeBannerBean.getData().getRecords();
            for (int i = 0; i < records.size(); i++) {
                HomeBannerBean.DataBean.RecordsBean recordsBean = records.get(i);
                int isMaster = recordsBean.getIsMaster();
                if(isMaster == id)
                    picUrl = recordsBean.getPicUrl();
            }
        }catch (Exception e){

        }

        return picUrl;
    }

    private int getMast(int id) {
        int isMaster = -1;
        try {
            if(homeBannerBean ==null)
                return isMaster;
            List<HomeBannerBean.DataBean.RecordsBean> records = homeBannerBean.getData().getRecords();
            for (int i = 0; i < records.size(); i++) {
                HomeBannerBean.DataBean.RecordsBean recordsBean = records.get(i);
                int beanId = recordsBean.getId();
                if(beanId == id)
                isMaster = recordsBean.getIsMaster();
            }

            return isMaster;

        }catch (Exception e){

        }

        return isMaster;
    }

    public void setBeanDatas(HomeBannerBean homeBannerBean) {

        this.homeBannerBean = homeBannerBean;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BannerViewHolder(@NonNull ImageView view) {
            super(view);
            this.imageView = view;
        }
    }
}
